package com.szzg10.webservice.chapter4;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class RestfulTeamsClient {

	public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
		
		//---GET---
		//HttpURLConnection connection = getConnection("http://localhost:9876/restfulTeams?key=Team1", "GET");
		
		//---POST---
		/*HttpURLConnection connection = getConnection("http://localhost:9876/restfulTeams", "POST");
		connection.setRequestProperty("cargo", getPostXml());
		connection.connect();*/
		
		//---PUT---
		//HttpURLConnection connection = getConnection("http://localhost:9876/restfulTeams?oldName=Team3&newName=Team-1", "PUT");
		
		//---DELETE---
		HttpURLConnection connection = getConnection("http://localhost:9876/restfulTeams?name=Team3", "DELETE");
		
		print_and_parse(connection);
	}
	
	public static HttpURLConnection getConnection(String strUrl, String verb) throws IOException {
		
		URL url = new URL(strUrl);
		HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		
		connection.setRequestMethod(verb);
		
		return connection;
	}
	
	public static void print_and_parse(HttpURLConnection connection) throws IOException, ParserConfigurationException, SAXException {
		
		String xml = "";
		BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		
		String next = null;
		while((next = reader.readLine()) != null) {
			xml += next;
		}
		
		System.out.println("The raw XML:\n" + xml);
		System.out.println();
		
		SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
		saxParser.parse(new ByteArrayInputStream(xml.getBytes()), new SaxParserHandler());
	}
	
	private static String getPostXml() {
		return "<create_team>" +
			"<name>Team3</name>" +
			"<player>" +
				"<name>Ideal</name>" +
			"</player>" +
			"<player>" +
				"<name>Torch</name>" +
			"</player>" +
		"</create_team>";
	}
}

class SaxParserHandler extends DefaultHandler {
	char[] buffer = new char[1024];
	int n = 0;
	
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		clear_buffer();
	}
	
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		System.arraycopy(ch, start, buffer, 0, length);
		n += length;
	}
	
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if(Character.isUpperCase(buffer[0])) {
			System.out.println(new String(buffer));
		}
		
		clear_buffer();
	}
	
	private void clear_buffer() {
		Arrays.fill(buffer, '\0');
		n = 0;
	}
}
